package cp3120.ucm.client;

import cp3120.ucm.*;
import cp3120.dao.DAOFactory;
import cp3120.model.Credentials;
import cp3120.view.MainUI;
import cp3120.model.User;
import cp3120.net.AirportClient;

/**
 * @author Adam Scarr
 * @author Rowan Spence
 * @since r1
 *
 * UC LOGIN USER
 * PRECONDITION: The system has taken the User's credentials and the User has request to login<br>
 * POST-CONDITION: The User is logged in <br>
 * USER: A customer wishing to log into their account<br>
 * CREDENTIALS: The User's username and pwassword<br>
 * 1. System verifies customer credentials<br>
 * 2. System loads User's details from the database<br>
 * 3. System logs user in and makes them the active user<br>
 * 4. System updates the Main UI<br>
 * 1a. System could not verify credentials and displays an error
 */
public class UCLoginUser implements UCController {
    private MainUI ui = MainUI.getInstance();
    private Credentials credentials;

    public UCLoginUser(Credentials credentials) {
        this.credentials = credentials;
    }

    /**
     * runs this UC
     */
    public void run() {
        User user = (User)AirportClient.getInstance().sendRequest("AccountManagerService", "authenticateUser", credentials).getResponse().getPayload();

        if (user == null) {
            ui.getLoginDialogue().setErrorVisible(true);
            ui.getLoginDialogue().open();
            return;
        }

        AirportClient.getInstance().setUser(user);
        ui.update();
    }
}
